package com.m.controller;

import com.m.dao.entity.LoginEntity;
import com.m.enums.WebReturnCode;
import com.m.service.ICommonService;
import com.m.utils.ResponseBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 功能：<br/>
 *
 * @author miracle
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@RestController
@RequestMapping("/api/v1/auth")
@Api(tags = "登录")
public class CommonController {
    private static final Logger LOG = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private ICommonService commonService;

    @ApiOperation(value = "登录")
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<LoginEntity> login(@RequestParam(value = "userName", required = true) String userName,
                                             @RequestParam(value = "passwd", required = true) String passwd) {
        try {
            return ResponseBuilder.build(WebReturnCode.SUCCEED,"success", commonService.login(userName, passwd));
        } catch (Exception e) {
            LOG.error("登录失败:" + e.getMessage(), e);
            return ResponseBuilder.build(WebReturnCode.FAILED,"登录失败:" + e.getMessage(), null);
        }
    }

    @ApiOperation(value = "退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity logout(@RequestParam(value = "userId", required = true) Long userId) {
        try {
            commonService.logout(userId);
            return ResponseBuilder.build(WebReturnCode.SUCCEED,"success", null);
        } catch (Exception e) {
            LOG.error("退出登录失败:" + e.getMessage(), e);
            return ResponseBuilder.build(WebReturnCode.FAILED,"退出登录失败:" + e.getMessage(), null);
        }
    }

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    @ResponseBody
    public void hello() {
        System.out.println("123");
    }
}
